﻿using System.Diagnostics;
using System.Reflection;
using Abp.AspNetCore.Configuration;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Modules;
using Abp.Runtime.Session;
using Magicodes.WeChat.Authorization;
using Magicodes.WeChat.Configuration;
using Magicodes.WeChat.Core;
using Magicodes.WeChat.SDK;
using Magicodes.WeChat.SDK.Builder;
using Magicodes.WeChat.WeChatApiSetting.Dto;
using Newtonsoft.Json;
using System;
using Magicodes.WeChat.Application.Startup;
using Abp.Runtime.Caching;

namespace Magicodes.WeChat.Application
{
    [DependsOn(
        typeof(WeChatCoreModule),
        typeof(AbpAutoMapperModule))]
    public class WeChatApplicationModule : AbpModule
    {
        public WeChatApplicationModule()
        {
        }


        public override void PreInitialize()
        {
            //如果useConventionalHttpVerbs设置为true,  HTTP 方法将根据方法名称的约定来匹配:
            //HttpGet: 方法名以"Get"作为前缀
            //Put: Used if method name starts with 'Put' or 'Update'.
            //Delete: Used if method name starts with 'Delete' or 'Remove'.
            //Post: Used if method name starts with 'Post', 'Create' or 'Insert'.
            //Patch: Used if method name starts with 'Patch'.
            //Otherwise, Post is used as default HTTP verb.
            Configuration.Modules.AbpAspNetCore()
                .CreateControllersForAppServices(typeof(WeChatApplicationModule).Assembly, "weChat", true);
            //添加权限程序
            Configuration.Authorization.Providers.Add<WeChatAuthorizationProvider>();

            //配置指定的Cache过期时间
            Configuration.Caching.Configure("WeChatSetting", cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
            });
        }

        public override void PostInitialize()
        {
            base.PostInitialize();
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            var _cacheManager = IocManager.Resolve<ICacheManager>();
            //配置微信SDK
            WeChatSDKConfigHelper.Config(Logger, _cacheManager);
        }
    }
}